package org.jiaxin.nutrient.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;

/**
 * @Author: jiaxin
 * @Description : TODO 对于 文件的读取 统计 工具类
 * @date: 2022/7/19 15:17
 **/

public class FileUtils {

    /**
     *
     * @param filePath
     * @return String
     * @Explianation 按照行读取数据，并将文件所有的数据放到一个字符串里
     */
    public static String getFileContentFromLocalPath(String filePath) throws Exception {
        InputStreamReader inputStreamReader = new InputStreamReader(Files.newInputStream(new File(filePath).toPath()), StandardCharsets.UTF_8);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line="";
        StringBuilder stringBuilder=new StringBuilder();

        while ((line=bufferedReader.readLine())!=null){
            stringBuilder.append(line).append("\n");
        }
        return stringBuilder.toString();
    }

    /**
     *
     * @param filePath
     * @param delimiter
     * @return List<String>
     * @throws Exception
     * @Explianation 根据指定的分隔符 返回所有的数据 并放到List 集合里
     */
    public static List<String> readFileFromLocalPathToList(String filePath,String delimiter) throws Exception {
        if (StringUtils.isNotBlank(filePath) && StringUtils.isNotBlank(delimiter))
            return new ArrayList<>(Arrays.asList(getFileContentFromLocalPath(filePath).split(delimiter)));
        return null;
    }

    /**
     *
     * @param aList
     * @param keys
     * @return Map<String,Integer>
     * @Explianation 统计某个集合中 某个元素出现的频率,且元素可以传入多个参数
     */

    /**
     * TODO key为文件的行号,value为按照分隔符分割的List集合
     * @param filePath
     * @param delimiter
     * @return
     * @throws Exception
     */
    public static Map<Long,List<String>> getFileMapFromLocalPath(String filePath, String delimiter) throws Exception {
        if (StringUtils.isBlank(filePath)){
            throw new Exception("输入的文件地址不能为空！！！！");
        }
        return getFileMapLimitNum(filePath,Integer.MAX_VALUE,delimiter);
    }

    public static Map<Long,List<String>> getFileMapFromLocalPath(String filePath) throws Exception {
        if (StringUtils.isBlank(filePath)){
            throw new Exception("输入的文件地址不能为空！！！！");
        }
        return getFileMapFromLocalPath(filePath,null);
    }






    public static Map<Long,List<String>> getFileMapLimitNum(String filePath, long lineNum,String delimiter) throws Exception {
        return getFileWithRange(filePath,0,lineNum,delimiter);
    }



    public static Map<Long,List<String>> getFileWithRange(String filePath,long startLine,long endLine,String delimiter) throws Exception {
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(new File(filePath)), StandardCharsets.UTF_8);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line="";
        HashMap<Long, List<String>> hashMap = new HashMap<>();
        long count=0L;
        if (startLine>endLine){
            throw new Exception("startLine 不能大于 endLine");
        }

        while ((line=bufferedReader.readLine())!=null){
            if (count>=startLine && count<endLine){
                List<String> lineList = new ArrayList<>();
                if (delimiter==null || delimiter.equals("")){
                    lineList.add(line);
                }else {
                    lineList = new ArrayList<>(Arrays.asList(line.split(delimiter)));
                }
                hashMap.put(count,lineList);
                count++;
                continue;
            }
            if (count==endLine){
                break;
            }



            count++;
        }
        return hashMap;
    }









}
